package com.zwh.se.collection.set;



import com.zwh.se.common.pojo.POJO;
import com.zwh.se.common.pojo.Student;

import java.util.HashSet;
import java.util.Set;

/**
 * @author yexi
 */
public class UseHashSetWhenUseObject {
    public static void main(String[] args) {
        Set<Student> students = new HashSet<>();
        //对象的地址是不一样的,所以无论对象的内容有多么雷同都是加得进去set的(即便对象的信息完全相同也一样)
        //若想能够解决这个问题,就得重写比较规则(equals)和哈希码(hashCode)
        //本对象已完成以上任务
        students.add(new Student(1L, "will", 17));
        students.add(new Student(2L, "lucy", 18));
        students.add(new Student(3L, "stef", 19));
        students.add(new Student(3L, "stef", 19));
        students.add(new Student(4L, "allen", 20));
        students.add(new Student(4L, "allen", 20));
        System.out.println(students.size());
        //本对象未完成以上任务
        Set<POJO> pojos = new HashSet<>();
        pojos.add(new POJO(1L, "大狗", 30));
        pojos.add(new POJO(1L, "大狗", 30));
        pojos.add(new POJO(1L, "大狗", 30));
        pojos.add(new POJO(1L, "大狗", 30));
        pojos.add(new POJO(1L, "大狗", 30));
        pojos.add(new POJO(1L, "大狗", 30));
        pojos.add(new POJO(1L, "大狗", 30));
        System.out.println(pojos.size());
    }
}
